const {
  createArticle,
  getArticleList,
  getArticleById,
  deleteArticleById,
  updateArticle,
} = require("../service/article");

module.exports = {
  async create(ctx) {
    ctx.verifyParams({
      categoryId: "string",
      title: "string",
      content: "string",
      summary: "string",
    });

    // 创建文章
    await createArticle(ctx, ctx.request.body);

    ctx.body = {
      code: 0,
      message: "创建成功",
      data: true,
    };
  },

  async list(ctx) {
    const result = await getArticleList(ctx, ctx.query);

    ctx.body = {
      code: 0,
      message: "获取文章列表成功",
      data: result,
    };
  },

  async detail(ctx) {
    const result = await getArticleById(ctx, ctx.params.id);

    console.log(111, new Object(ctx.params.id));

    ctx.body = {
      code: 0,
      message: "获取文章详情成功",
      data: result,
    };
  },

  async delete(ctx) {
    await deleteArticleById(ctx, ctx.params.id);

    ctx.body = {
      code: 0,
      message: "删除成功",
      data: true,
    };
  },

  async update(ctx) {
    ctx.verifyParams({
      categoryId: "string",
      title: "string",
      content: "string",
      summary: "string",
    });

    const id = ctx.params.id;

    // 更新文章
    await updateArticle(ctx, id, ctx.request.body);

    ctx.body = {
      code: 0,
      message: "更新成功",
      data: true,
    };
  },
};
